package com.ivy.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.ivy.dao.MessageDao;
import com.ivy.entity.Friend;
import com.ivy.entity.Message;
import com.ivy.utils.JdbcUtils;

public class MessageDaoImpl implements MessageDao {

	@Override
	public void insert(Message message) {
		String sql = "insert into message(sender,receiver,content,type) values(?,?,?,?);";
		QueryRunner qr = JdbcUtils.getQueryRunner();
		
		try {
			qr.update(sql,message.getSender(),message.getReceiver(),message.getContent(),message.getType());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Message> findMessage(Message message) {
		//	select * from message where owner_tel = '1233' and other_tel = '1336';   where sender = '"+ message.getSender() +"' and receiver = '"+ message.getReceiver() +"'
		String sql = "select * from message;";
		List<Message> messages = null;
		try {
			QueryRunner qr = JdbcUtils.getQueryRunner();
			messages = qr.query(sql, new BeanListHandler<Message>(Message.class));
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		
		return messages;
	}

}
